//
//  CatListService.m
//  HongYanNews
//
//  Created by 123 on 14-7-16.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "CatListService.h"

@interface CatListService()
{
    ASIFormDataRequest *request_;
}
@end

@implementation CatListService

- (void)dealloc {
    [request_ cancel];
    
    NSLog(@"CatListService dealloc...");
}

- (void)pageQueryWithSelectedIndex:(NSInteger)selectedIndex {
    
    if (_isQuery || ([self.pageInfo.pageno integerValue] > 1 && !self.pageInfo.hasMoreData)) {
        return;
    }
    
    /* 获取全部类别 */
    
    // 检查网络
    if (![Constants isExistenceNetwork]) {
        
    } else {
        request_ = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:API_STR]];
        request_.requestMethod = @"POST";
        // 设置超时时间
        request_.timeOutSeconds = 30;
        request_.delegate = self;
        // 设置参数
        NSString *action;
        switch (selectedIndex) {
            case 0://新闻
                action = @"newstypelist";
                break;
            case 1://人才
                action = @"zhaopintypelist";
                break;
            case 2://视频
                action = @"videotypelist";
                break;
            case 3://产品
                action = @"prostypelist";
                break;
            case 4://图片
                action = @"casetypelist";
                break;
            case 5://服务
                action = @"serinfortypelist";
                break;
    
            default:
                break;
        }
        //NSDictionary *paramsDict =  @{@"uid": @"互联天下",@"pagenow": self.pageInfo.pageno, @"pagesize": self.pageInfo.pagesize};
        NSDictionary *paramsDict =  @{@"uid": UNAME,@"pagenow": self.pageInfo.pageno, @"pagesize": self.pageInfo.pagesize};
        NSString *paramsStr = [NSString JSONString:paramsDict];
        NSString *result = [NSString stringWithFormat:TEMPLATE_PARAMSTR, action,paramsStr];
        
        NSData *paramsData = [result toGBKEncodingData];
        request_.postBody = [paramsData mutableCopy];
        
        [request_ startAsynchronous];
        
        
        _isQuery = YES;
    }
}

#pragma mark - Property method
- (CatPageInfo*)pageInfo{
    if (!_pageInfo) {
        _pageInfo = [[CatPageInfo alloc]init];
    }
    return _pageInfo;
}

#pragma mark - ASIHttpRequestDelegate method

- (void)requestFinished:(ASIHTTPRequest *)request {
    NSLog(@"%@",request.responseString);
    NSError *err;
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[request.responseString dataForUTF8] options:NSJSONReadingAllowFragments error:&err];
    
    // 1. 是否解析有错误
    if (!err) {
        
        // 2. 判断错误码
        NSInteger errorCode = [result[@"errorCode"]integerValue];
        if (errorCode == 0) {
            // 2.1 获取"info"
            NSDictionary *info = result[@"info"];
            //NSLog(@"%@",info);
            
            //NSInteger totalCount = [info[@"total"]integerValue];
            NSArray *list = info[@"data"];
            
            if ([self.pageInfo.pageno isEqualToString:@"1"]) {
                [self.pageInfo.dataArr removeAllObjects];
            }
            [self.pageInfo.dataArr addObjectsFromArray:list];
            
            //self.pageInfo.count = totalCount;
            
            //            if (self.pageInfo.hasMoreData) {
            //                [self.pageInfo nextPage];
            //            }
            [_delegate didRecieveData];
            
            
        } else if (errorCode == 5){
            // [self.pageInfo.dataArr removeAllObjects];
            self.pageInfo.hasMoreData = NO;
            [_delegate didRecieveMsg];
        }else {
            
            NSString *dataStr = result[@"info"][@"data"];
            if ([dataStr isEqualToString:@"0"]) {
                [self.pageInfo.dataArr removeAllObjects];
                self.pageInfo.hasMoreData = NO;
                if ([result[@"errorMsg"] isEqualToString:@"该条件下为得到任何数据"]){
                    [_delegate didRecieveMsg];
                }
            }
        }
        
    } else {
        [SVProgressHUD showErrorWithStatus:@"数据解析出错！"];
        [_delegate didRecieveMsg];
    }
    
    _isQuery = NO;
}

- (void)requestFailed:(ASIHTTPRequest *)request{
    [SVProgressHUD showErrorWithStatus:@"服务器无法访问，请稍后再试!"];
    [_delegate didRecieveMsg];
    _isQuery = NO;
}

// 取消网络访问
- (void)cancelRequest {
    [request_ cancel];
}

@end
